package com.wrial.seckill.service.impl;
/*
 * @Author  Wrial
 * @Date Created in 11:48 2020/4/4
 * @Description
 */

import com.wrial.seckill.dao.UserDOMapper;
import com.wrial.seckill.dao.UserPasswordDOMapper;
import com.wrial.seckill.dataobject.UserDO;
import com.wrial.seckill.dataobject.UserPasswordDO;
import com.wrial.seckill.error.CustomizeErrorCode;
import com.wrial.seckill.error.CustomizeException;
import com.wrial.seckill.service.UserService;
import com.wrial.seckill.service.model.UserModel;
import com.wrial.seckill.validator.ValidationResult;
import com.wrial.seckill.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

@Service
@CrossOrigin(origins = {"*"},allowCredentials = "true")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDOMapper userDOMapper;
    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;
    // 注入校验器
    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel getUserById(int id) {
        UserDO userDO = userDOMapper.selectById(id);
        String password = userPasswordDOMapper.getPasswordByUserId(id);
        UserModel model = convert(userDO, password);
        return model;
    }

    @Override
    public void register(UserModel userModel) {
        if (userModel == null) throw new CustomizeException(CustomizeErrorCode.PARAMETER_VALIDATION_ERROR);


        ValidationResult result = validator.validate(userModel);
        if (result.isHasErrors()) {
            throw new CustomizeException(CustomizeErrorCode.PARAMETER_VALIDATION_ERROR.setMessage(result.getErrMsg()));
        }
        //实现model->dataobject方法
        UserDO userDO = convert(userModel);
        try{
            userDOMapper.insert(userDO);
        }catch(DuplicateKeyException ex){
            throw new CustomizeException(CustomizeErrorCode.PARAMETER_VALIDATION_ERROR.setMessage("手机号已重复注册"));
        }

        userModel.setId(userDO.getId());
        UserPasswordDO userPasswordDO = convertPasswordFromModel(userModel);
        userPasswordDOMapper.insert(userPasswordDO);
        return;
    }

    private UserPasswordDO convertPasswordFromModel(UserModel userModel){
        if(userModel == null){
            return null;
        }
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }
    @Override
    public UserModel validateLogin(String telphone, String encrptPassword) {
        //通过用户的手机获取用户信息
        UserDO userDO = userDOMapper.selectByTelphone(telphone);
        if(userDO == null){
            throw new CustomizeException(CustomizeErrorCode.USER_LOGIN_FAIL);
        }
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
        UserModel userModel = convertFromDataObject(userDO,userPasswordDO);

        //比对用户信息内加密的密码是否和传输进来的密码相匹配
        if(!StringUtils.equals(encrptPassword,userModel.getEncrptPassword())){
            throw new CustomizeException(CustomizeErrorCode.USER_LOGIN_FAIL);
        }
        return userModel;
    }
    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO){
        if(userDO == null){
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO,userModel);

        if(userPasswordDO != null){
            userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        }

        return userModel;
    }

    private UserDO convert(UserModel userModel) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    private UserModel convert(UserDO userDO, String password) {
        UserModel model = new UserModel();
        BeanUtils.copyProperties(userDO, model);
        model.setEncrptPassword(password);
        return model;
    }

}
